package org.lgiavedoni.android.franq.core.place;

import java.util.ArrayList;
import java.util.List;

import org.lgiavedoni.android.franq.core.Constants;
import org.lgiavedoni.android.franq.core.position.Position;
import org.lgiavedoni.android.franq.util.googe.MapsUtil;

public class Place extends Position {

	private String name = null;
	private final List<Permanence> permanences = new ArrayList<Permanence>();
	private boolean speakHere = true;

	public Place(String name, double latitude, double longitude) {
		super(latitude, longitude);
		this.name = name;
	}

	public Place(double latitude, double longitude) {
		super(latitude, longitude);
	}

	public Place(Position position) {
		super(position.getLatitude(), position.getLongitude());
		setNetworks(position.getNetworks());
		setCellLocation(position.getCellLocation());
	}

	public String getName() {
		return name != null ? name : Constants.UNKNOWN;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void addPermanence() {
		permanences.add(new Permanence(this));
	}

	public void addPermanence(Permanence permanence) {
		if (permanences.size() > 0 && !permanences.get(permanences.size() - 1).isCompleted()) {
			permanences.get(permanences.size() - 1).end();
		}
		permanences.add(permanence);
	}

	public void finishPermanence() {
		if (permanences != null && permanences.size() > 0)
			permanences.get(permanences.size() - 1).end();
	}

	public boolean isUnknowName() {
		return name == null;
	}

	@Override
	public String toString() {
		if (name != null)
			return "Place [name=" + name + "]";
		return super.toString();
	}

	@Override
	public String getMapURL() {
		return MapsUtil.getURL(this);
	}

	public List<Permanence> getPermanences() {
		return permanences;
	}

	public Permanence getLastPermanence() {
		return permanences != null && permanences.size() > 0 ? permanences.get(permanences.size() - 1) : null;
	}

	public boolean isSpeakHere() {
		return speakHere;
	}

	public void setSpeakHere(boolean speakHere) {
		this.speakHere = speakHere;
	}

}
